<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考核结果" prop="assessmentResult">
        <el-select v-model="queryParams.assessmentResult" placeholder="请选择考核结果">
          <el-option
            v-for="item in assessmentResultOptionsQuery"
            :key="item.id"
            :label="item.label"
            :value="item.value"
            clearable
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="searchTime" label="考核年月">
        <el-date-picker size="small"
                        v-model="queryParams.searchTime"
                        type="month"
                        value-format="yyyy-MM"
                        @change="handleQuery"
                        placeholder="选择年月" style="width:250px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="generateAppraisalForm"
          v-hasPermi="['personnelMatters:personnelMatters:generateOfMonth']"
        >生成当月考核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['personnelMatters:personnelMatters:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personnelMattersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="考核编号" align="center" prop="personAssessmentId"/>
      <el-table-column label="用户姓名" align="center" prop="nickName"/>
      <el-table-column label="身份证号" align="center" prop="userIdcardnum"/>
      <el-table-column label="考核结果" align="center" prop="assessmentResult">
        <template slot-scope="{row}">
          <el-tag
            type="info"
            v-show="row.assessmentResult==='0'"
          >
            未考核
          </el-tag>
          <el-tag
            type="success"
            v-show="row.assessmentResult==='1'"
          >
            优秀
          </el-tag>
          <el-tag
            type="success"
            v-show="row.assessmentResult==='2'"
          >
            合格
          </el-tag>
          <el-tag
            type="success"
            v-show="row.assessmentResult==='3'"
          >
            基本合格
          </el-tag>
          <el-tag
            type="danger"
            v-show="row.assessmentResult==='4'"
          >
            不合格
          </el-tag>
          <el-tag
            type="danger"
            v-show="row.assessmentResult==='5'"
          >
            不定等次
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="考核年度" align="center" prop="assessmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assessmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="assessmentResultInfo"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            class="pan-btn green-btn"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['personnelMatters:personnelMatters:edit']"
          >审核
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或审核人事考核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户姓名" prop="nickName">
          <el-input v-model="form.nickName" :disabled="form.personAssessmentId != undefined"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="userIdcardnum">
          <el-input v-model="form.userIdcardnum" :disabled="form.personAssessmentId != undefined"
          />
        </el-form-item>
        <el-form-item label="考核年份" prop="assessmentTime">
          <el-date-picker clearable
                          v-model="form.assessmentTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :disabled="form.personAssessmentId != undefined"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考核结果" prop="assessmentResult">
          <el-select v-model="form.assessmentResult" placeholder="请选择考核结果">
            <el-option
              v-for="item in assessmentResultOptions"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核评语" prop="assessmentComment">
          <el-input v-model="form.assessmentComment" type="textarea" placeholder="请输入考核评语"/>
        </el-form-item>

        <el-form-item label="备注" prop="personRemarks">
          <el-input v-model="form.personRemarks" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>

      <el-dialog
        width="30%"
        title="二次确认"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-descriptions title="用户信息">
          <el-descriptions-item label="用户姓名">{{ form.nickName }}</el-descriptions-item>
          <el-descriptions-item label="考核年份">{{ form.assessmentTime }}</el-descriptions-item>
          <el-descriptions-item label="考核结果">{{ secondResult }}</el-descriptions-item>
          <el-descriptions-item label="描述">
            <a style="color:red" v-show="form.assessmentResult==='4'||form.assessmentResult==='5'">当前选择为【{{
                secondResult
              }}】,{{ secondResultInfo }},点击【确认】将进行提交,点击【取消】返回重新选择</a>
            <a style="color:red"
               v-show="form.assessmentResult==='1'||form.assessmentResult==='2'||form.assessmentResult==='3'"
            >当前选择为【{{ secondResult }}】,将【{{ secondResultInfo }}】,确认后薪级变动立即生效,操作不可逆，请再次确认。<br/>点击【确认】将进行提交,点击【取消】返回重新选择</a>
          </el-descriptions-item>
        </el-descriptions>


        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmSubmitForm">确 定</el-button>
          <el-button @click="innerVisible=false">取 消</el-button>
        </div>
      </el-dialog>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPersonnelMatters,
  getPersonnelMatters,
  delPersonnelMatters,
  addPersonnelMatters,
  updatePersonnelMatters, generateAppraisalForm
} from '@/api/personnelMatters/personnelMatters'
import second from '@/components/Crontab/second'

export default {
  name: 'PersonnelMatters',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人事考核表格数据
      personnelMattersList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        assessmentResult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' }
        ],
        userIdcardnum: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        assessmentResult: [
          { required: true, message: '考核结果不能为空', trigger: 'blur' }
        ]
      },
      assessmentResultOptions: [
        {
          id: 1,
          value: '1',
          label: '优秀'
        },
        {
          id: 2,
          value: '2',
          label: '合格'
        },
        {
          id: 3,
          value: '3',
          label: '基本合格'
        },
        {
          id: 4,
          value: '4',
          label: '不合格'
        },
        {
          id: 5,
          value: '5',
          label: '不定等次'
        },
        {
          id: 0,
          value: '0',
          label: '未考核'
        }
      ],
      assessmentResultOptionsQuery: [

        {
          id: 1,
          value: '1',
          label: '优秀'
        },
        {
          id: 2,
          value: '2',
          label: '合格'
        },
        {
          id: 3,
          value: '3',
          label: '基本合格'
        },
        {
          id: 4,
          value: '4',
          label: '不合格'
        },
        {
          id: 5,
          value: '5',
          label: '不定等次'
        },
        {
          id: 0,
          value: '0',
          label: '未考核'
        }
      ],

      /*二次确认弹出框 */
      innerVisible: false,
      secondResult: null,
      secondResultInfo: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询人事考核列表 */
    getList() {
      this.loading = true
      listPersonnelMatters(this.queryParams).then(response => {
        this.personnelMattersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        personAssessmentId: null,
        userId: null,
        nickName: null,
        userIdcardnum: null,
        assessmentResult: '1',
        assessmentComment: null,
        assessmentTime: null,
        personRemarks: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personAssessmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加人事考核'
    },
    /** 审核按钮操作 */
    handleUpdate(row) {
        this.reset()
        const personAssessmentId = row.personAssessmentId || this.ids
        getPersonnelMatters(personAssessmentId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '审核人事考核'
        })
    },

    /** 确认按钮 */
    submitForm() {
      this.secondResult = null
      this.secondResultInfo = null
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.personAssessmentId != null) {
            if (this.form.assessmentResult === '0') {
              this.alertMsg('当前未选择考核结果')
            } else {
              this.innerVisible = true
              if (this.form.assessmentResult === '1') {
                this.secondResult = '优秀'
                this.secondResultInfo = '增加一级薪级'
              } else if (this.form.assessmentResult === '2') {
                this.secondResult = '合格'
                this.secondResultInfo = '增加一级薪级'
              } else if (this.form.assessmentResult === '3') {
                this.secondResult = '基本合格'
                this.secondResultInfo = '增加一级薪级'
              } else if (this.form.assessmentResult === '4') {
                this.secondResult = '不合格'
                this.secondResultInfo = '薪级不变'
              } else if (this.form.assessmentResult === '5') {
                this.secondResult = '不定等次'
                this.secondResultInfo = '薪级不变'
              }
            }
          }
        }
      })
    },

    /** 二次确认提交按钮 */
    confirmSubmitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updatePersonnelMatters(this.form).then(response => {
            this.$modal.msgSuccess('审核成功')
            this.open = false
            this.innerVisible = false
            this.getList()
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('personnelMatters/personnelMatters/export', {
        ...this.queryParams
      }, `考核记录表${new Date().getTime()}.xlsx`)
    }
    ,

    generateAppraisalForm() {
      generateAppraisalForm().then(response => {
        this.$modal.msgSuccess(response.msg)
        this.getList()
      })
    }
    ,

    //弹出提示
    alertMsg(title) {
      this.$alert(this.tipMsg, title, {
        confirmButtonText: '确定',
        showConfirmButton: false
      }).catch(() => {
      })
    }
  }
}
</script>
